package com.xyzk.diagnose.view.dialog;

import java.util.ArrayList;

import com.xyzk.diagnose.R;
import com.xyzk.diagnose.activity.adapter.SelectAdapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.PopupWindow;


@SuppressLint("InflateParams")
public class CPopupWindow extends PopupWindow{
	private GridView gridview;
	private Activity context;

	/**
	 * @param context 上下文
	 * @param data 数据
	 * @param colums gridview的列数
	 */
	public CPopupWindow(Activity context, ArrayList<String> data) {
		super(context);
		this.context = context;
		initData(data);
	}

	private void initData(ArrayList<String> data){
		View view = LayoutInflater.from(context).inflate(R.layout.popupwindow, null);
		gridview  = (GridView) view.findViewById(R.id.gv_popupwindow);
		gridview.setAdapter(new SelectAdapter(data));
		if(data != null){
			if(data.size() < 7){
				gridview.setNumColumns(1);
			}else if(data.size() < 13){
				gridview.setNumColumns(2);
				if(data.size() % 2 != 0){
					data.add("");//填充没数据样式
				}
			}else if(data.size() < 19){
				gridview.setNumColumns(3);
				for(int i = 0; i < 3 - (data.size() % 3); i ++ ){
					if(data.size() % 3 == 0) break;
					data.add("");//填充没数据样式
				}
			}else{
				gridview.setNumColumns(4);
				for(int i = 0; i < 4 - (data.size() % 4); i ++ ){
					if(data.size() % 4 == 0) break;
					data.add("");//填充没数据样式
				}
			}
		}

		setFocusable(false);// 这个很重要
		setOutsideTouchable(false);

		setContentView(view);
		DisplayMetrics metric  = new DisplayMetrics();
		context.getWindowManager().getDefaultDisplay().getMetrics(metric);;
		this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);// 设置弹出窗口的高
		this.setWidth((int) (metric.widthPixels * ((data.size() < 7) ? 0.4 : 0.6)));
	}


	/**
	 * 回调处理
	 * @param l
	 */
	public void onClickListener(OnItemClickListener click){
		gridview.setOnItemClickListener(click);
	};
}
